package api

import (
	"context"
	"fmt"
	"net/http"

	"wms/internal/svc"
	"wms/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AllLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAllLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AllLogic {
	return &AllLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AllLogic) All() (resp *types.ApisResponse, err error) {
	resp = new(types.ApisResponse)

	fmt.Println("查询所有api")
	apis, err := l.svcCtx.ApiModel.FindByPage(l.ctx, "")
	if err != nil {
		fmt.Printf("[Error]查询全部api:%s\n", err.Error())
		resp.Code = http.StatusInternalServerError
		resp.Msg = "服务内部错误"
		return resp, nil
	}

	resp.Data = make([]types.ApiItem, 0)
	for _, one := range apis {
		resp.Data = append(resp.Data, types.ApiItem{
			Id:       one.Id,
			Type:     one.Type,
			SortId:   one.SortId,
			ParentId: one.ParentId,
			Uri:      one.Uri,
			Method:   one.Method,
			Name:     one.Name,
			Remark:   one.Remark,
		})
	}

	resp.Code = http.StatusOK
	resp.Msg = "成功"
	return resp, nil
}
